package first.niuke.simple;

/**
 * @ClassName NC19
 * @description:
 * @author: 荣燊
 * @create: 2021-04-12 11:31
 **/
public class NC19 {
    public static void main(String[] args) {
        int[] i = {1, 2, -99, 5, -5};
        System.out.println(maxsumofSubarray(i));
    }

    public static int maxsumofSubarray (int[] arr) {
        // write code here
        // dp[i] = dp[i-1] + arr[i];
        int[] dp = new int[arr.length];
        dp[0] = arr[0];
        int max = dp[0];
        for (int i=1; i<arr.length; i++) {
            if (dp[i-1] < 0) {
                dp[i] = arr[i];
            } else {
                dp[i] = dp[i-1] + arr[i];
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }
}
